
#include <BFont>
#include <BWidget>
#include "member_BLineEdit.h"

using namespace BWE;

member_BLineEdit::member_BLineEdit(BLineEdit* edit)
{
	boss = edit;
	readOnly = false;
	blinkTime = 500;
	blinkTick = 0;
	editing = false;
	pressed = false;
	align = Align_Left | Align_VCenter;
	hoverIndex = 0;
	inputIndex = 0;
	inputCount = 0;
	inputShown = false;
	dirty = true;
}
member_BLineEdit::~member_BLineEdit()
{

}

void member_BLineEdit::freahText()
{
	text.reset();
	for (int bi = 0; bi < cells.size(); bi++)
	{
		text.append(cells[bi].code);
	}
}

void member_BLineEdit::freshRects()
{
	const BFont* font = boss->font();
	if (!font)
		return;

	BRect crect = boss->clientRect();
	BSize textSize = font->textSize(text);
	textRect = crect.align(align, textSize);

	int rowHeight = font->height();

	int fontWidth = font->width();
	int fontHeight = font->height();
	int up = fontHeight * 0.2;
	int x = textRect.x();
	int y = textRect.y();
	cells.resize(text.count());
	for (auto it = text.begin(); it != text.end(); it++)
	{
		CodeCell& cell = cells[it.count()];
		cell.code = it.value();
		cell.fresh(font, rowHeight);
		cell.rect.setPos(x, y);
		cell.image_rect.setPos(x + cell.image_left, cell.rect.bottom() - cell.image_top - up);
		x += cell.rect.width();
	}

	if (inputIndex < 0)
		inputIndex = 0;
	if (inputIndex > cells.size())
		inputIndex = cells.size();
}
